package com.iepacj.zk;

import org.apache.zookeeper.*;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

/**
 * Created by iEpac on 16/4/9.
 */
public class ZkTest2 {


    private CountDownLatch cdl = new CountDownLatch(1);

    public static void main(String[] args) {

        ZkTest2 zkTest2 = new ZkTest2();
        zkTest2.go();


    }

    private void go() {

        String connStr = new String("localhost:2181");

        try {
            ZooKeeper zk = new ZooKeeper(connStr, 3000, new ZKWatcher());

            cdl.await();

            String path1 = zk.create("/zk-test-", "123".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
            System.out.println("创建节点: " + path1);


            String path2 = zk.create("/zk-test-", "123".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
            System.out.println("创建节点: " + path2);

            Thread.sleep(1000 * 60 * 60 * 24);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (KeeperException e) {
            e.printStackTrace();
        }

    }


    class ZKWatcher implements Watcher {

        @Override
        public void process(WatchedEvent event) {

            System.out.println("收到watcher事件: " + event);

            if (Event.KeeperState.SyncConnected == event.getState()) {
                System.out.println("连接建立...");
                cdl.countDown();
            }


        }
    }


}




























